package classifier;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TextFileReader {

	private static final String TRAININGSET_FILE = "./lib/TrainingSet_old.txt";
	private static final String OPTION_FILE = "./lib/OptiesText.txt";
	private static final String CAT_FILE = "./lib/CatText.txt";

	// Indexes used to parse the training sets
	private static final int CATNAME = 9;
	private static final int FIRST_INDEX = 0;
	private static final int LAST_INDEX = 8;
	
	// The FeatureType used by all features : yes/no
	private static final FeatureType YESNO = new FeatureType("YESNO",new String[]{"0","1"});
	
	
	// The Collections
	private String[] options;

	private Map<Item, String> trainingsSet;
	private Map<String, FeatureType> features;
	
	// The readers
	private BufferedReader trainingReader;
	private BufferedReader catReader;
	private BufferedReader optionReader;
	
	
	public TextFileReader() throws IOException{
		init();
		this.trainingsSet = new HashMap<Item, String>();
		this.features	=	new	HashMap<String, FeatureType>();
		this.options = parseOptions();
		parseItems();
	}
	
	private void init(){
		try {
			this.trainingReader = new BufferedReader(new FileReader(TRAININGSET_FILE));
			this.catReader = new BufferedReader(new FileReader(CAT_FILE));
			this.optionReader = new BufferedReader(new FileReader(OPTION_FILE));
		} catch (FileNotFoundException e) {
			System.err.println("TextFileReader : Something went wrong with trying to create the reader.");
			e.printStackTrace();
		}
	}
	
	private void parseItems() throws IOException{
		String line;
		
		String tmp = trainingReader.readLine();
		tmp = trainingReader.readLine();
		
		while((line = trainingReader.readLine()) != null){
			String[] item = line.split(";");
			
			String name = item[CATNAME];
			Feature[] features = getFeatures(item);
			
			System.out.println(Arrays.toString(features));
			Item itm = new Item(name, features);
			this.trainingsSet.put(itm,name);
	
		}
		

	}
	
	private Feature[] getFeatures(String[] featureValues) {
		ArrayList<Feature> buffer = new ArrayList<Feature>();
		
		for(int i = FIRST_INDEX; i<LAST_INDEX; i++){
			Feature f = new Feature(this.options[i],featureValues[i+1],YESNO);
			buffer.add(f);
			this.features.put(this.options[i], YESNO);
		}	

		Feature[] returnVal = new Feature[buffer.size()];
		buffer.toArray(returnVal);
		return returnVal;
		
	}
	
	
	private String[] parseOptions() throws IOException{
		ArrayList<String> temp = new ArrayList<String>();
		
		String line;
		while((line = optionReader.readLine()) != null){
			temp.add(line);
		}
		
		String[] returnValue = new String[temp.size()];
		temp.toArray(returnValue);
		
		return returnValue;
		
	}
	
	// getters
	public String[] getOptions() {
		return this.options;
	}
	
	public Map<Item, String> getTrainingsSet() {
		return this.trainingsSet;
	}

	public Map<String, FeatureType> getFeatures() {
		return this.features;
	}

}
